<?php
namespace backend\controllers;
//use backend\controllers\RbacController;
use yii\web\Controller;
use backend\models\TmeMerchandiseinfo;
use Yii;
use yii\db\Query;
use backend\models\MerchandisForm;
use backend\models\TmeMerchandisecinfo;
use backend\models\TmeProstatusinfo;
use yii\helpers\ArrayHelper;
use backend\models\TmeUnitinfo;
use backend\views\merchandis\merchandiseab_class;
use backend\extention_class\zhuji;
use yii\web\UploadedFile;
use backend\assets\Unitinfo;

/**
 * #name=商品信息表#
 */
class MerchandisController extends Controller {
	
    public $layout = 'ace';
  	public $enableCsrfValidation = false;
  	
	/**
	 * #operation=默认操作#
	 */
    public function actionIndex(){
	
    /* $listdate=new zhuji();
	$listdate->getInitials("光明"); */		
	     $merchandisinfomodel = new MerchandisForm(['scenario'=>'merchandisinfoadd']);

	     //自动生成助记码
		 if('ab'== \Yii::$app->request->post('action')){		 				
			$names = \Yii::$app->request->post('names');                                     //获取商品名称   		  		
			return $this->renderPartial('zhuji',['names'=>$names]);			
		}
		
		 //文件上传操作及添加数据操作		 		
		if(\Yii::$app->request->isPost && 'merchandisadd'==\Yii::$app->request->post('action')) {

			 /* var_dump($_FILES['MerchandisForm']['name']['PicPath']);
			 exit; */
					//$nan = yii::$app->request->post('MerchandisForm')['PicPath'] = $_FILES['MerchandisForm']['name']['PicPath'];										
				    $merchandisinfomodel->MerchandiseID = \Yii::$app->request->post('MerchandisForm')['MerchandiseID'];
					$merchandisinfomodel->UnitID = \Yii::$app->request->post('MerchandisForm')['UnitID'];
					$merchandisinfomodel->MerchandiseName = \Yii::$app->request->post('MerchandisForm')['MerchandiseName'];
					$merchandisinfomodel->MerchandiseAB = \Yii::$app->request->post('MerchandisForm')['MerchandiseAB'];
					$merchandisinfomodel->Price = \Yii::$app->request->post('MerchandisForm')['Price'];
					$merchandisinfomodel->SaleStatus = \Yii::$app->request->post('MerchandisForm')['SaleStatus'];
					$merchandisinfomodel->Spec = \Yii::$app->request->post('MerchandisForm')['Spec'];
					$merchandisinfomodel->Describes = \Yii::$app->request->post('MerchandisForm')['Describes'];
					$merchandisinfomodel->PicPath = UploadedFile::getInstance($merchandisinfomodel,'PicPath');      //图片上传
					$merchandisinfomodel->ClickCount = \Yii::$app->request->post('MerchandisForm')['ClickCount'];
					$merchandisinfomodel->MerchandiseCID = \Yii::$app->request->post('MerchandisForm')['MerchandiseCID'];
					$merchandisinfomodel->ProStatusID = \Yii::$app->request->post('MerchandisForm')['ProStatusID'];
					$merchandisinfomodel->Remark = \Yii::$app->request->post('MerchandisForm')['Remark'];
								
			       //$merchandisinfomodel->file = UploadedFile::getInstance($merchandisinfomodel,'file');$merchandisinfomodel->PicPath->saveAs('uploads/'.time().'.jpg');
			       //保存数据到数据库
			if($merchandisinfomodel->validate()){
				
                 $path = $_FILES['MerchandisForm']['name']['PicPath'];
				   //图片保存
				//$merchandisinfomodel->PicPath->saveAs('uploads/'.time().'.jpg');
                  $merchandisinfomodel->PicPath->saveAs('uploads/'.$path.'');
                                   
			      $merchandisemodel = new TmeMerchandiseinfo();
				   
			      $merchandisemodel->MerchandiseID = $merchandisinfomodel->MerchandiseID;
			      $merchandisemodel->UnitID = $merchandisinfomodel->UnitID;
			      $merchandisemodel->MerchandiseName = $merchandisinfomodel->MerchandiseName;
			      $merchandisemodel->MerchandiseAB = $merchandisinfomodel->MerchandiseAB;
			      $merchandisemodel->Price = $merchandisinfomodel->Price;
			      $merchandisemodel->SaleStatus = $merchandisinfomodel->SaleStatus;
			      $merchandisemodel->Spec = $merchandisinfomodel->Spec;
			      $merchandisemodel->Describes = $merchandisinfomodel->Describes;
			      $merchandisemodel->PicPath = $path;
			      
			      $merchandisemodel->ClickCount = $merchandisinfomodel->ClickCount;
			      $merchandisemodel->MerchandiseCID = $merchandisinfomodel->MerchandiseCID;
			      $merchandisemodel->ProStatusID = $merchandisinfomodel->ProStatusID;
			      $merchandisemodel->Remark = $merchandisinfomodel->Remark;
			      
			     /*  var_dump($_FILES['MerchandisForm']['tmp_name']);
			      exit;                  		 */	  				  			  
		       if($merchandisemodel->save()){	
		      
					echo "添加成功！";	
					$this->redirect(['merchandis/index']);
				}else{ 
					echo "添加失败！";					
				}
			}											
	   }						 	   
						
			$unitinfo = new TmeUnitinfo();
			$unitiresult = TmeUnitinfo::find('UnitID >0')->All();
			$listdate = ArrayHelper::map($unitiresult, 'UnitID', 'Name');              //提取单位数据
			$prostatus = new TmeProstatusinfo();
			$prostatusresult = TmeProstatusinfo::find('ProStatusID >0')->all();
			$listdate1 = ArrayHelper::map($prostatusresult, 'ProStatusID','ProStatusName');
			
			return $this->render('index',[
					'merchandisinfomodel'=>$merchandisinfomodel,
					'listdate'=>$listdate,
					'listdate1'=>$listdate1,
					
	          ]);
		}

      //
      public function actionMerchautodata(){
     	
      	$value = \Yii::$app->request->post('value');     	
      	$merchandisR = TmeMerchandisecinfo::find()->select('MerchandiseCID')->where("MerchandiseCID like '%".$value."%' ")->all();
      	if(!$merchandisR){    		
      		echo "没有此类别";
      	}else{
      		echo $value;
      	}     	     	        
     } 
        
	/**
	 * #operation=商品信息查看#
	 */
	public function actionMerchandisinfoview(){
		
		
		if('merchandisview_action' == \Yii::$app->request->post('action')){
			$id = \Yii::$app->request->post('id');
			$merchandismodel = new TmeMerchandiseinfo();
			$merchandisresult = TmeMerchandiseinfo::findOne(['ID'=>$id]);
		
										
				echo "<table style=\"width:300px;height:400px\"><tr style=\"cellspacing:0;margin-right:30px\">";
				echo "<td style=\"width:230px;height:20px\">商品编码:&nbsp;&nbsp;".$merchandisresult->MerchandiseID."</td>";								
			    echo "<td>单位编码:&nbsp;&nbsp;".$merchandisresult->unit->Name."</td>";
			    echo "</tr><tr>";					
		        if($merchandisresult->ProStatusID==1){
					echo "<td>促销状态:&nbsp;&nbsp;<span style=\"color:green\">促销中</span></td>";					
				}else{
					echo "<td>促销状态:&nbsp;&nbsp;<span style=\"color:red\">非促销中</span></td>";
				}		
				echo "<td>商品类别编码:&nbsp;&nbsp;".$merchandisresult->MerchandiseCID."</td>";
				echo "</tr><tr>";
				echo "<td>商品名称:&nbsp;&nbsp;".$merchandisresult->MerchandiseName."</td>";
				echo "<td>商品助记码:&nbsp;&nbsp;".$merchandisresult->MerchandiseAB."</td>";
				echo "</tr><tr>";
				echo "<td>商品价格:&nbsp;&nbsp;".$merchandisresult->Price."</td>";
				//echo "<td>销售状态:&nbsp;&nbsp;&nbsp;&nbsp;".$merchandisresult->SaleStatus."</td>";
				if($merchandisresult->SaleStatus==1){
					echo "<td>销售状态:&nbsp;&nbsp;<span style=\"color:green\">销售中</span></td>";					
				}else{
					echo "<td>销售状态:&nbsp;&nbsp;<span style=\"color:red\">非销售中</span></td>";
				}								
				echo "</tr><tr>";
				echo "<td>商品规格:&nbsp;&nbsp;".$merchandisresult->Spec."</td>";
				
				if($merchandisresult->proStatus->Status==1){
					echo "<td>活动名称:&nbsp;&nbsp;<span style=\"color:red\">".$merchandisresult->proStatus->ProStatusName."</span></td>";
				}else{
					echo "<td>活动名称:&nbsp;&nbsp;无</td>";
				}
				echo "</tr><tr>";
				echo "<td>图片路径:&nbsp;&nbsp;".$merchandisresult->PicPath."</td>";
				echo "<td>点击量:&nbsp;&nbsp;".$merchandisresult->ClickCount."</td>";
				echo "</tr><tr>";
				echo "<td>商品描述:&nbsp;&nbsp;".$merchandisresult->Describes."</td>";
				echo "<td>备注:&nbsp;&nbsp;".$merchandisresult->Remark."</td>"; 
				echo "</tr></table>";				
		}		
	}
	/**
	 * #operation=商品信息修改#
	 */
	public function actionMerchandisupdate(){
		
		//修改数据并写入数据
		if(\Yii::$app->request->isPost && 'updateM' == \Yii::$app->request->post('action')){
			
			
		    $merchandisinfomodel = new MerchandisForm(['scenario'=>'merchandisinfoadd']);
		    
	            $merchandisinfomodel->MerchandiseID = \Yii::$app->request->post('MerchandisForm')['MerchandiseID'];
				$merchandisinfomodel->UnitID = \Yii::$app->request->post('MerchandisForm')['UnitID'];
				$merchandisinfomodel->MerchandiseName = \Yii::$app->request->post('MerchandisForm')['MerchandiseName'];
				$merchandisinfomodel->MerchandiseAB = \Yii::$app->request->post('MerchandisForm')['MerchandiseAB'];
				$merchandisinfomodel->Price = \Yii::$app->request->post('MerchandisForm')['Price'];
				$merchandisinfomodel->SaleStatus = \Yii::$app->request->post('MerchandisForm')['SaleStatus'];
				$merchandisinfomodel->Spec = \Yii::$app->request->post('MerchandisForm')['Spec'];
				$merchandisinfomodel->Describes = \Yii::$app->request->post('MerchandisForm')['Describes'];
				$merchandisinfomodel->PicPath = UploadedFile::getInstance($merchandisinfomodel,'PicPath');      //图片上传
				$merchandisinfomodel->ClickCount = \Yii::$app->request->post('MerchandisForm')['ClickCount'];
				$merchandisinfomodel->MerchandiseCID = \Yii::$app->request->post('MerchandisForm')['MerchandiseCID'];
				$merchandisinfomodel->ProStatusID = \Yii::$app->request->post('MerchandisForm')['ProStatusID'];
				$merchandisinfomodel->Remark  = \Yii::$app->request->post('MerchandisForm')['Remark'];
				
			       //$merchandisinfomodel->file = UploadedFile::getInstance($merchandisinfomodel,'file');$merchandisinfomodel->PicPath->saveAs('uploads/'.time().'.jpg');
			       //保存数据到数据库
				var_dump($merchandisinfomodel->validate());
			//if($merchandisinfomodel->validate()){
				
				   //图片保存
				 $path = $_FILES['MerchandisForm']['name']['PicPath'];
			     $merchandisinfomodel->PicPath->saveAs('uploads/'.$path.'');
									      
			      $merchandised = new TmeMerchandiseinfo();
			      $id = $merchandisinfomodel->MerchandiseID;
			      $merchandisemodel = TmeMerchandiseinfo::findOne(['MerchandiseID'=>$id]);
			      
			      $merchandisemodel->MerchandiseID = $merchandisinfomodel->MerchandiseID;
			      $merchandisemodel->UnitID = $merchandisinfomodel->UnitID;
			      $merchandisemodel->MerchandiseName = $merchandisinfomodel->MerchandiseName;
			      $merchandisemodel->MerchandiseAB = $merchandisinfomodel->MerchandiseAB;
			      $merchandisemodel->Price = $merchandisinfomodel->Price;
			      $merchandisemodel->SaleStatus = $merchandisinfomodel->SaleStatus;
			      $merchandisemodel->Spec = $merchandisinfomodel->Spec;
			      $merchandisemodel->Describes = $merchandisinfomodel->Describes;
			      $merchandisemodel->PicPath = $path;
			      
			      $merchandisemodel->ClickCount = $merchandisinfomodel->ClickCount;
			      $merchandisemodel->MerchandiseCID = $merchandisinfomodel->MerchandiseCID;
			      $merchandisemodel->ProStatusID = $merchandisinfomodel->ProStatusID;
			      $merchandisemodel->Remark = $merchandisinfomodel->Remark;
			      			                 				  			  
		       if($merchandisemodel->save()){	
		      
					echo "修改成功！";	
					$this->redirect(['merchandis/index']);
				}else{ 
					echo "修改失败！";					
				}
			}											
	 //  }				
															
		//自动生成助记码
		if('abc' == \Yii::$app->request->post('action')){
			$name = \Yii::$app->request->post('name');
			$zhuji = new zhuji();
			$zhuM = strtolower($zhuji->getInitials($name));
			echo $zhuM;
		}
		
		if(\Yii::$app->request->isPost  && 'merchandis_update' == \Yii::$app->request->post('action')){
			
			$unitinfo = new TmeUnitinfo();
			$unitiresult = TmeUnitinfo::find('UnitID >0')->All();
			$listdata = ArrayHelper::map($unitiresult, 'UnitID', 'Name');              //提取单位数据
			$prostatus = new TmeProstatusinfo();
			$prostatusresult = TmeProstatusinfo::find('ProStatusID >0')->all();
			$listdata1 = ArrayHelper::map($prostatusresult, 'ProStatusID','ProStatusName');
			
			$id = \Yii::$app->request->post('id');
			$merchandismodel = new MerchandisForm(['scenario'=>'merchandisinfoadd']);
			$merchandisresult = new TmeMerchandiseinfo();
			$result = TmeMerchandiseinfo::findOne(['ID'=>$id]);	
			
			$MerchandiseID = $result->MerchandiseID;			
			$UnitID = $result->UnitID;
			$ProStatusID = $result->ProStatusID;
			$MerchandiseCID = $result->MerchandiseCID;
			$MerchandiseName = $result->MerchandiseName;
			$MerchandiseAB = $result->MerchandiseAB ;
			$Price = $result->Price ;
			$SaleStatus = $result->SaleStatus ;
			$Spec = $result->Spec ;
			$Describes = $result->Describes ;
			$PicPath = $result->PicPath ;
			$ClickCount = $result->ClickCount ;
			$Remark = $result->Remark ;
								
		  return $this->renderPartial('_merchandisinfoupdate',['merchandismodel'=>$merchandismodel,
		  		'listdata'=>$listdata,
		  		'listdata1'=>$listdata1,
		  		'MerchandiseID'=>$MerchandiseID,
                'UnitID'=>$UnitID,
		  		'ProStatusID'=>$ProStatusID,
		  		'MerchandiseCID'=>$MerchandiseCID,
		  		'MerchandiseName'=>$MerchandiseName,
		  		'MerchandiseAB'=>$MerchandiseAB,
		  		'Price'=>$Price,
		  		'SaleStatus'=>$SaleStatus,
		  		'Spec'=>$Spec,
		  		'Describes'=>$Describes,
		  		'PicPath'=>$PicPath,
		  		'ClickCount'=>$ClickCount,
		  		'Remark'=>$Remark				           
           ]);
		}						
	}
	
	/**
	 * #operation=商品信息删除操作#
	 */
	public function actionMerchandisinfodele(){
		
		$id = \Yii::$app->request->post('id');
		$merchandisdel = new TmeMerchandiseinfo();
		$resultdel = TmeMerchandiseinfo::findOne(['ID'=>$id])->delete(); 
	      if($resultdel){
	      	echo "删除成功！";
	      	$this->redirect(['merchandis/index']);
	      }else{
	      	echo "删除失败！";
	      }
	}
	
	
	/**
	 * #operation=商品信息数据调取#
	 */
	public function actionDatainfo(){
        
		$draw = \Yii::$app->request->post('draw');
		$length = \Yii::$app->request->post('length');
		$start = \Yii::$app->request->post('start');
		$column = \Yii::$app->request->post('order')[0]['column'];
		$dir = \Yii::$app->request->post('dir')[0]['dir'];
		$search = \yii::$app->request->post('search')['value'];
		
		
		$columnArr=[
	      '0'=>'MerchandiseID',  //商品编码
	      '2'=>'ProStatusID',    //促销状态码
	      '4'=>'Price',          //价格
	      '9'=>'ClickCount'      //点击量		
		];		
		$columnN=$columnArr[$column];
		
		//搜索框
		if(!empty($search)){
			$value=$search;
			$where="MerchandiseID like '%".$value."%' or ProStatusID like '%".$value."%' or MerchandiseName like '%".$value."%' or MerchandiseAB like '%".$value."%' or Price like '%".$value."%' or SaleStatus like '%".$value."%' or Spec like '%".$value."%' or PicPath like '%".$value."%' ";
		}else{
			$where="";
		}
		
		
		$date = new TmeMerchandiseinfo();		
		$result = TmeMerchandiseinfo::find()->where($where)->orderBy($columnN." ".$dir)->limit($length)->offset($start)->createCommand()->queryAll();
		$total = TmeMerchandiseinfo::find()->where($where)->count();
      
        
		
	    $data=[];
		foreach ($result as $v){
			$tempv=[];
			$tempv['chose']='<label class="pos-rel"> <input type="checkbox"
							 class="ace"> <span class="lbl"></span>';
			$tempv['MerchandiseID']=$v['MerchandiseID'];
			$tempv['ProStatusID']=$v['ProStatusID'];
			$tempv['MerchandiseName']=$v['MerchandiseName'];
			$tempv['MerchandiseAB']=$v['MerchandiseAB'];
			$tempv['Price']=$v['Price'];
			$tempv['SaleStatus']=$v['SaleStatus'];
			$tempv['Spec']=$v['Spec'];
			$tempv['PicPath']="<img style=\"width:55px;height:40px\" src=uploads/".$v['PicPath'].">"; 
			$tempv['ClickCount']=$v['ClickCount'];
			$tempv['options']='<div class="hidden-sm hidden-xs action-buttons">        
																<a class="blue" onclick="return merchandisview('.$v['ID'].')" href="#">
																	<i class="ace-icon fa fa-search-plus bigger-130"></i>
																</a>

																<a class="green" onclick="return merchandisupdate('.$v['ID'].')" href="#">
																	<i class="ace-icon fa fa-pencil bigger-130"></i>
																</a>

																<a class="red" onclick="return merchandisdele('.$v['ID'].')" href="#">
																	<i class="ace-icon fa fa-trash-o bigger-130"></i>
																</a>
															</div>';
			
			$data[]=$tempv;						
		}
		
		$temp=[
		'draw'=>$draw,
		'recordsTotal'=>$total,
		'recordsFiltered'=>$total,
		'data'=>$data		
		];
		echo json_encode($temp); 		
	} 
	
	
	/**
	 * #operation=商品分类信息#
	 */
	public function actionMerchandissortshow(){
	  
			 		 
	 	 return $this->render('merchandissortshow');
	} 
	
	/**
	 * #operation=商品分类数据ztree数据调取#
	 */
	public function actionZtree(){
		
		if(\Yii::$app->request->isPost){

			$data = new TmeMerchandisecinfo();
			$result = TmeMerchandisecinfo::find()->All();			
			$temp=[];
			foreach($result as $c){
				$temp[]=[
					'id'=>$c['ID'],
					'pId'=>$c['MerchandisePID'],
					'name'=>$c['MerchandiseCName']		
				];
			}
			echo json_encode($temp);
			exit;
		}
		return $this->render('index');
		}
		
         /**
          * #operation=添加商品#
          */
		public function actionId(){
			
		    if(\Yii::$app->request->isPost && 'Aadd' == \Yii::$app->request->post('action') ){
	      		$merchandissortmodel = new MerchandisForm(['scenario'=>'merchandisadd']);	      		
	      		$merchandissortmodel->attributes = \Yii::$app->request->post('MerchandisForm');
	      		//var_dump($merchandissortmodel->attributes);
	      		//if($merchandissortmodel->validate()){	      			
	      			$sortaddmodel = new TmeMerchandisecinfo();   				     	
	      			$sortaddmodel->attributes = $merchandissortmodel->attributes;
	      			
	      			if($sortaddmodel->save()){
	      				echo "添加分类商品成功";
	      				$this->redirect(['merchandis/merchandissort']);
	      			}else{
	      				echo "失败";
	      			}	      			
		    }
			
				$id = \yii::$app->request->post('id')?\yii::$app->request->post('id'):1001;
				$merchandissortmodel = new MerchandisForm(['scenario'=>'merchandisadd']);
				//$id = 1001;
			    if('Aadd_action' == \Yii::$app->request->post('action')){	      	
		      	$id =  \Yii::$app->request->post('id');
		     	//$mid='123';
		     	//var_dump($id);
		     
				return $this->renderPartial('_merchandissotadd',['merchandissortmodel'=>$merchandissortmodel,'id'=>$id]);
		     }
		  // return $this->renderPartial('_merchandissotadd',['merchandissortmodel'=>$merchandissortmodel,'id'=>$id]);		
		}
			 
	/**
	 * #operation=商品分类及查看#
	 */
    public function actionMerchandissort(){
    	
    	          //$updatamodel = new MerchandisForm(['scenario'=>'merchandisadd']);
			
		    	  $id = \yii::$app->request->post('id')?\yii::$app->request->post('id'):1003;  	  
		    	  $merchandissortmodel = new MerchandisForm(['scenario'=>'merchandisadd']);
		    	  
		    	  /* $arr = \Yii::$app->request->post('MerchandisForm');
		    	   if(!empty($arr['MerchandisePID'])){  	  	
		    	  	exit("<script>alert('确定选择商品类别？');window.location.href=\"index.php?r=merchandis/merchandissort\"</script>");
		    	  }
				    */
		    	 		    	  
				  $updatamodel = new MerchandisForm(['scenario'=>'merchandisadd']);
				  	
				  $sortmodel = TmeMerchandisecinfo::findOne(['ID'=>$id]);
				  
				  	
				  $ID=$sortmodel->ID;
				  
				  $MerchandiseCID=$sortmodel->MerchandiseCID;
				  
				  $MerchandisePID=$sortmodel->MerchandisePID;
				  
				  $MerchandiseCName=$sortmodel->MerchandiseCName;
				  //MerchadiseCName
				  $SortID=$sortmodel->SortID;
				  $State=$sortmodel->State;
		  
		return $this->render('merchandissortshow',[
				'merchandissortmodel'=>$merchandissortmodel,'id'=>$id,
				'updatamodel'=>$updatamodel,
				'ID'=>$ID,
			    'MerchandiseCID'=>$MerchandiseCID,
			    'MerchandisePID'=>$MerchandisePID,
			    'MerchandiseCName'=>$MerchandiseCName,
			    'SortID'=>$SortID,
			    'State'=>$State             				
            ]);
        	
     }  
	
		
	/**
	 * #operation=商品类别修改#
	 */
	 public function actionModify(){
		
		//商品类别	 	
	 	if(\Yii::$app->request->isPost && 'update' == \Yii::$app->request->post('action')){
	 		 		
		 		$updatemodel = new MerchandisForm();
		 		$updatemodel->scenario="merchandisupdate";
		 		
		 		$updatemodel->attributes = \Yii::$app->request->post('MerchandisForm');	 		
		 		$id=$updatemodel->ID;	 		
		 	    $update = TmeMerchandisecinfo::findOne(['ID'=>$id]);	 	    
		 	    $update->ID= $updatemodel->ID;	 	    
		 	    $update->MerchandiseCID=$updatemodel->MerchandiseCID; 	   
		 	    $update->MerchandisePID= $updatemodel->MerchandisePID;
		 	    $update->MerchandiseCName=$updatemodel->MerchandiseCName;	 	   
		 	    $update->SortID=$updatemodel->SortID;
		 	    $update->State=$updatemodel->State;
	 	    
	 	    if($update->save()){	 	    	
	 	    	echo "<script>alert(\"数据修改成功！\")</script>";
	 	    	$this->redirect(['merchandis/merchandissort']);
	 	    }else{	 	    	
	 	    	throw new \Exception("数据修改失败", 101);
	 	    }	 		 	
	 	}
	 
	 	 //判断接收ajax传输id
		 if('modify_action'==\Yii::$app->request->post('action')){
		 	
			  $id= \Yii::$app->request->post('id');		 
			  $updatamodel = new MerchandisForm(['scenario'=>'merchandisadd']);		 
			  $sortmodel = TmeMerchandisecinfo::findOne(['ID'=>$id]);
			  		 
			  $ID=$sortmodel->ID; 			  
			  $MerchandiseCID=$sortmodel->MerchandiseCID;		  
			  $MerchandisePID=$sortmodel->MerchandisePID;		  
			  $MerchandiseCName=$sortmodel->MerchandiseCName;		 								//MerchadiseCName
			  $SortID=$sortmodel->SortID;
			  $State=$sortmodel->State;
			 
			 return $this->renderPartial('_merchandissortupdate',[
			   'updatamodel'=>$updatamodel,	
		       'ID'=>$ID,
			   'MerchandiseCID'=>$MerchandiseCID,
			   'MerchandisePID'=>$MerchandisePID,
			   'MerchandiseCName'=>$MerchandiseCName,
			   'SortID'=>$SortID,
			   'State'=>$State		 		
			 ]);
		  
		/* var_dump(\Yii::$app->request->post('action'));
	    if(\Yii::$app->request->isPost && 'update' == \Yii::$app->request->post('action')){
	    	
	     } */			
		} 						
	}
	
	/**
	 * #operation=删除商品操作#
	 */
  public  function actionDele(){
		  	$id=\Yii::$app->request->post('id');
		  	$merchandissortmodel = new MerchandisForm();
		  	
		  	if(\Yii::$app->request->isPost && 'dele'==\Yii::$app->request->post('action')){
		  		$ID = \Yii::$app->request->post('MerchandisForm')['ID']; 		
		  		$dele = new TmeMerchandisecinfo();
		  		$delete = $dele::findOne(['ID'=>$ID])->delete();
		  		if($delete){
		  		  echo "删除成功！";
		  		  $this->redirect(['merchandis/merchandissort']);
		  	 }else{ 
		  		  echo "删除失败！";
		  		}	 		
		  	}  	
	  	  return $this->renderPartial('_merchandissortdele',['merchandissortmodel'=>$merchandissortmodel,'id'=>$id]);
	 }
	
	
	/**
	 * #operation=商品促销状态展示#
	 */
  public function actionProstatus(){
	 	
	 	$prostatusadd = new MerchandisForm(['scenario'=>'prostatusadd']);	 	
	 	//添加促销状态	 	
	 	if(\Yii::$app->request->isPost && 'prostatus'== \Yii::$app->request->post('action')){

	 		$prostatusadd->attributes = \Yii::$app->request->post('MerchandisForm');
	 	if($prostatusadd->validate()){
	 		$arpro = new TmeProstatusinfo();
	 		$arpro->attributes = $prostatusadd->attributes;
	 		if($arpro->save()){
	 		     echo "添加促销状态成功！";
	 		     $this->redirect(['merchandis/prostatus']);
	 	  }else{ 
	 			throw new \Exception("添加促销状态失败！", 1);
	 	    }
	     }			
	  }	 		 	 	
	 	return  $this->render('prostatus',['prostatusadd'=>$prostatusadd]);
	} 
	 
	 /**
	  * #operation=商品促销查看#
	  */
	public function actionProstatusview(){
		//var_dump($_POST);
		if('prostatusview_action' == \Yii::$app->request->post('action')){
			
			$id = \Yii::$app->request->post('id');
			
			$prostatu = new TmeProstatusinfo();
			$result = TmeProstatusinfo::find('ProStatusID >=0')->where(['ProStatusID'=>$id])->all();
			$temp="";
			foreach($result as $v){
				$temp .="促销状态编码:&nbsp;&nbsp;&nbsp;&nbsp;".$v['ProStatusID']."<hr/>";
				$temp .="促销状态名称:&nbsp;&nbsp;&nbsp;&nbsp;".$v['ProStatusName']."<hr/>";			
				if($v['Status']==0){
					$temp .="促销状态:&nbsp;&nbsp;&nbsp;&nbsp;非活动中<hr/>";
				}else{					
					$temp .="<p style='color:red'>促销状态:&nbsp;&nbsp;&nbsp;&nbsp;活动中</p><hr/>";
				}
				$temp .="备注:&nbsp;&nbsp;&nbsp;&nbsp;".$v['Remark']."<hr/>";
			}			
			echo $temp;			
		}				
	} 
	
	/**
	 * #operation=商品促销修改#
	 */
	public function actionProstatusmodify(){
		
		//判断并执行修改动作
		if('prostatusupdate' == \Yii::$app->request->post('action')){
			
			$updatemodel = new MerchandisForm(['scenario'=>'prostatusmodify']);			
			$updatemodel->attributes = \Yii::$app->request->post('MerchandisForm');			
			$id = $updatemodel->ProStatusID; 
			$prostatusresult = TmeProstatusinfo::findOne(['ProStatusID'=>$id]);
		
			$prostatusresult->ProStatusID =  $updatemodel->ProStatusID;
			$prostatusresult->ProStatusName = $updatemodel->ProStatusName;
			$prostatusresult->Status =  $updatemodel->Status;
			$prostatusresult->Remark = $updatemodel->Remark;
					
		    if($prostatusresult->save()){
		    	
		    	echo "修改数据成功！";
		    	$this->redirect(['merchandis/prostatus']);
		    }else{	    	
		    	echo "修改失败！";
		    }
		}
       
		//传值到form视图脚本
		if('prostatusmodify_action'==\Yii::$app->request->post('action')){
			$prostatusmodel = new MerchandisForm(['scenario'=>'prostatusmodify']);
			$pro = new TmeProstatusinfo();
			$id = \Yii::$app->request->post('id');
			$prostatus = TmeProstatusinfo::findOne(['ProStatusID'=>$id]);
			$ProStatusID = $prostatus->ProStatusID;
			$ProStatusName = $prostatus->ProStatusName;
			$Status = $prostatus->Status;
			$Remark	= $prostatus->Remark;
		
		 return $this->renderPartial('_prostatusupdate',['prostatusmodel'=>$prostatusmodel,
		 		                                  'ProStatusID'=>$ProStatusID,
                                                  'ProStatusName'=>$ProStatusName,
		 		                                  'Status'=>$Status,
		 		                                  'Remark'=>$Remark		 		                                   
                ]);		
		} 				
	} 
	
	/**
	 * #operation=商品促销删除操作#
	 */
	public function actionProstatusdele(){
						
		if('prostatusdele_action' == \Yii::$app->request->post('action')){
			
			$id = \Yii::$app->request->post('id');
			$connect = \Yii::$app->db;
			
			$transaction = $connect->beginTransaction();
			try {
				$merchandisinfodel = TmeMerchandiseinfo::find()->where(['ProStatusID'=>$id])->all();
				foreach($merchandisinfodel as $v){
					if(!$v->delete())
					throw new \Exception('删除信息表失败',2);
				}
				$prostatusdel = TmeProstatusinfo::findOne(['ProStatusID'=>$id])->delete();
				if(!$prostatusdel)
					throw new \Exception('删除促销表数据失败',1);
					$transaction->commit();
					$this->redirect(['merchandis/prostatus']);
			} catch (Exception $e) {
				$transaction->rollBack();
			}																							
		}						
	}
	/**
	 * #operation=促销状态数据调取#
	 */	
	public function actionData(){
        
		$draw = \Yii::$app->request->post('draw');
		$length = \Yii::$app->request->post('length');
		$start = \Yii::$app->request->post('start');
		$column = \Yii::$app->request->post('order')[0]['column'];
		$dir = \Yii::$app->request->post('dir')[0]['dir'];
		$search = \yii::$app->request->post('search')['value'];
		
		
		/* $columnArr=[
		'0'=>'ProStatusID',	
		'1'=>'ProStatusID',		                          //商品编码
		'2'=>'ProStatusName',    //促销状态码
		'3'=>'Status',          //价格
		'4'=>'Remark'           //点击量
		];
		$columnN=$columnArr[$column];   */
		
		
		//搜索框
		   if(!empty($search)){
			$value=$search;
			$where="ProStatusID like '%".$value."%' or ProStatusName like '%".$value."%' or Status like '%".$value."%' or Remark like '%".$value."%'";
		}else{
			$where="";
		}   
			
		$query = new Query();
		$result = $query->select('*')->from('tme_prostatusinfo')->limit($length)->offset($start)->createCommand()->queryAll();
			
		//查询数据的总条数
		$total = $query->select('*')->from('tme_merchandisecinfo')->count();
		$data=[];
		foreach ($result as $v){
			$tempv=[];
			$tempv['chose']='<label class="pos-rel"> <input type="checkbox"
							 class="ace"> <span class="lbl"></span>';
			$tempv['ProStatusID']=$v['ProStatusID'];
			$tempv['ProStatusName']=$v['ProStatusName'];
			$tempv['Status']=$v['Status'];
			$tempv['Remark']=$v['Remark'];
			$tempv['options']='<div class="hidden-sm hidden-xs action-buttons">
									<a class="blue" onclick="return view('.$v['ProStatusID'].')" href="#">
										<i class="ace-icon fa fa-search-plus bigger-130"></i>
									</a>

									<a class="green" onclick="return modify('.$v['ProStatusID'].')" href="#">
										<i class="ace-icon fa fa-pencil bigger-130"></i>
									</a>

									<a class="red" onclick="return dele('.$v['ProStatusID'].')" href="#">
										<i class="ace-icon fa fa-trash-o bigger-130"></i>
									</a>
								</div>';				
			$data[]=$tempv;
		}
			
		$temp=[
			'draw'=>$draw,
			'recordsTotal'=>$total,
			'recordsFiltered'=>$total,
			'data'=>$data,						
		];			
		echo json_encode($temp);									
	}
	
	
			
	/**
	 * #operation=商品单位字典管理#
	 */	
	public function actionUnitinfo(){
		$unitinfomodel = new MerchandisForm(['scenario'=>'unitinfo']);
	      
		
	    //单位添加
	    if(\Yii::$app->request->isPost && 'unitinfo' == \Yii::$app->request->post('action')){
	    	
	    	$unitinfomodel->attributes = \Yii::$app->request->post('MerchandisForm');
	    	if($unitinfomodel->validate()){
	    		$unitmodel = new TmeUnitinfo();
	    		$unitmodel->attributes = $unitinfomodel->attributes;	    			    		
	    			    		
	    		if($unitmodel->save()){
	    			echo "添加成功！";
	    		    $this->redirect(['merchandis/unitinfo']);
	    		}else{ 
	    			echo "添加失败！";
	    		}
	    	}
	    
	    }
	
		return $this->render('unitinfo',['unitinfomodel'=>$unitinfomodel]);	
	}
	
	/**
	 * #operation=商品单位数据获取#
	 */
	public function actionUnitinfodata(){
		
		$draw = \Yii::$app->request->post('draw');
		$length = \Yii::$app->request->post('length');
		$start = \Yii::$app->request->post('start');
		$column = \Yii::$app->request->post('order')[0]['column'];
		$dir = \Yii::$app->request->post('dir')[0]['dir'];
		$search = \yii::$app->request->post('search')['value'];
		
		
		$columnArr=[
		'0'=>'UnitID',         //编码
		'1'=>'UnitID',         //编码
		'2'=>'Name',           //单位名称
		
		];
		$columnN=$columnArr[$column];
		
		//搜索框
		if(!empty($search)){
			$value=$search;
			$where="UnitID like '%".$value."%' or Name like '%".$value."%' or Status like '%".$value."%' or Remark like '%".$value."%' ";
		}else{
			$where="";
		}
		
				
		$unitinfo = new TmeUnitinfo();
		$unitinfodata = TmeUnitinfo::find('UnitID >0')->asArray()->where($where)->orderBy($columnN." ".$dir)->limit($length)->offset($start)->all();
		$totalcount = TmeUnitinfo::find('UnitID >0')->asArray()->count();		
						
		$data=[];
		foreach ($unitinfodata as $v){
			$tempv=[];
			$tempv['chose']='<label class="pos-rel"> <input type="checkbox"
							 class="ace"> <span class="lbl"></span>';
			$tempv['UnitID']=$v['UnitID'];
			$tempv['Name']=$v['Name'];
			
			if($v['Status']==0){
				$tempv['Status']='不应用';
			}else{
				$tempv['Status']='应用';
			}
			$tempv['Remark']=$v['Remark'];
			
			$tempv['options']='<div class="hidden-sm hidden-xs action-buttons">        
									<a class="blue" onclick="return viewU('.$v['UnitID'].')" href="#">
										<i class="ace-icon fa fa-search-plus bigger-130"></i>
									</a>
	
									<a class="green" onclick="return updateU('.$v['UnitID'].')" href="#">
										<i class="ace-icon fa fa-pencil bigger-130"></i>
									</a>
	
									<a class="red" onclick="return deleteU('.$v['UnitID'].')" href="#">
										<i class="ace-icon fa fa-trash-o bigger-130"></i>
									</a>
								</div>';				
			$data[]=$tempv;
		}
		
		$temp=[
			'draw'=>$draw,
			'recordsTotal'=>$totalcount,
			'recordsFiltered'=>$totalcount,
			'data'=>$data
		];
		echo json_encode($temp);				
	}
	
	/**
	 * #operation=商品单位查看#
	 */
	public function actionUnitinfoview(){
		
		$unitinfomodel = new TmeUnitinfo();
        $id = \Yii::$app->request->post('id');
       
		$unitinforesult = TmeUnitinfo::find()->where(['UnitID'=>$id])->one();
		
		echo "单位编码:&nbsp;&nbsp;&nbsp;".$unitinforesult['UnitID'];
		echo "<hr>";
		echo "单位名称:&nbsp;&nbsp;&nbsp;".$unitinforesult['Name'];
		echo "<hr>";		
		if($unitinforesult['Status']==0){
			echo "单位状态:&nbsp;&nbsp;&nbsp;<span>不应用</span>";			
		}else{
			echo "单位状态:&nbsp;&nbsp;&nbsp;<span style=\"color:green\">应用</span>";
		}
		echo "<hr>";
		echo "备注:&nbsp;&nbsp;&nbsp;".$unitinforesult['Remark'];
	}


	/**
	 * #operation=商品单位修改#
	 */
	public function actionUpdate(){
		$model = new MerchandisForm(['scenario'=>'unitinfo']);						//实例化表单模型
		$id = yii::$app->request->post('id');										//获取js中传递的ID
		$updateModel = TmeUnitinfo::findOne($id);									//根据ID查数据
		if(yii::$app->request->post('action') == 'update'){							
			return $this->renderPartial('_merchandisupdate',[						//传入视图
					'id'=>$id,
					'model'=>$model,
					'updateModel'=>$updateModel,
			]);
		}	
		if(yii::$app->request->isPost){												//判断是否有表单提交动作
			$updateModel->attributes = yii::$app->request->post('MerchandisForm');
			if($updateModel->update())												//执行更新动作
				$this->redirect(['merchandis/unitinfo']);	
			else 
				$this->redirect(['merchandis/unitinfo']);
		}
	}

	/**
	 * #operation=商品单位单条删除#/*
	 */
	public function actionDelete(){
				
		$id = yii::$app->request->post('id');									
		$connect = \Yii::$app->db;
		$transaction = $connect->beginTransaction();
		try {
			$merchandisinfod = TmeMerchandiseinfo::find()->where(['UnitID'=>$id])->all();
		   foreach ($merchandisinfod as $v){
		   	 if(!$v->delete())
		   	       throw new \Exception('删除信息表数据失败！',1);
		   }		   	
		   $model = TmeUnitinfo::findOne($id);		   
		   if(!$model->delete())
		   	       throw new \Exception('删除单位表失败！',3);		   
		      $transaction->commit();
		      $this->redirect(['merchandis/unitinfo']);			
		} catch (Exception $e) {
			$transaction->rollBack();
		}				
	}

}




?>